package com.examination.controller.projectModel;

import com.examination.dao.projectModel.ProjectDao;
import com.examination.dao.scheduleModel.ScheduleDao;
import com.examination.entity.Project;
import com.examination.service.projectModel.ProjectService;
import com.examination.util.JsonResult;
import com.examination.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(description = "项目接口")
@RestController
@RequestMapping("project")
@CrossOrigin
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @Autowired
    private ScheduleDao scheduleDao;

    @ApiOperation(value = "分页模糊查询项目信息" , notes = "测试数据:{\"pageNo\": 1,\n" +
            "\"pageSize\":10}")
    @PostMapping("/findProject")
    public Page<Project> find_project(@RequestBody Map map) {
        System.out.println(map.get("id"));
        Page<Project> page = new Page<Project>();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(projectService.total(map));
        page.setItems(projectService.findProject(map));
        return page;
    }


    @ApiOperation(value = "增加项目" , notes = "测试数据:{\"project_name\":\"围护结构-地下连续墙(06)\", \"start_time\":\"2019-06-11\", \"end_time\":\"2019-06-11\", \"gross\":25, \"station_id\":11, \"person\":200 }")
    @PostMapping("/insertProject")
    public JsonResult insertProject(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = projectService.insertProject(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "修改项目" , notes = "测试数据:{\"project_name\":\"围护结构-地下连续墙\", \"start_time\":\"2019-06-11\", \"end_time\":\"2019-06-11\", \"gross\":25, \"station_id\":11, \"person\":200,\n" +
            "\"id\":61 }")
    @PostMapping("/updateProject")
    public JsonResult updateProject(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = projectService.updateProject(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "删除项目" , notes = "测试数据:{\"id\":61}")
    @PostMapping("/deleteProject")
    public JsonResult deleteProject(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = projectService.deleteProject(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "修改项目进度信息" , notes = "测试数据:{\"accomplish_day\":1,\n" +
            "\"id\":61,\n" +
            "\"station_id\":20}")
    @PostMapping("/updateProjectAccomplish")
    public JsonResult updateProjectAccomplish(@RequestBody Map map) {
        scheduleDao.add_schedule(map);
        map.put("accomplish_month" , scheduleDao.find_accomplish_sum(map));
        JsonResult jsonResult = new JsonResult();
        int i = projectService.updateProjectAccomplish(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "下拉框查询对应站点的项目信息" , notes = "测试数据:{\"station_id\":10}")
    @PostMapping("/selectProject")
    public List<Project> selectProject(@RequestBody Map map) {
        return projectService.selectProject(map);
    }

    @ApiOperation(value = "根据id查询项目信息" , notes = "测试数据:{\"id\":61}")
    @PostMapping("/findProjectById")
    public Project findProjectById(@RequestBody Map map) {
        return projectService.findProjectById(map);
    }

}
